<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="线索二叉树、哈夫曼树、AVL树、伸展树、B树、B+树、B*树、R树、红黑树概念与总结">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构之高级树">
<meta property="og:url" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/index.html">
<meta property="og:site_name" content="Lu QiLin&#39;s Blog">
<meta property="og:description" content="线索二叉树、哈夫曼树、AVL树、伸展树、B树、B+树、B*树、R树、红黑树概念与总结">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/C:/Blog/myblog/source/_posts/datastructureadvancedtree/bitree.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/huffm_tree.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_1.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_2.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_ll.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_rr.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_lr.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/avl_tree_rl.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree-insert-1.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree-insert-2.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree-insert-3.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree-delete.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-tree-disk.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-plus-tree.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/b-star-stree.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/R-tree.jpg">
<meta property="og:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/rb-tree.png">
<meta property="og:updated_time" content="2019-06-28T00:34:12.168Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据结构之高级树">
<meta name="twitter:description" content="线索二叉树、哈夫曼树、AVL树、伸展树、B树、B+树、B*树、R树、红黑树概念与总结">
<meta name="twitter:image" content="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/C:/Blog/myblog/source/_posts/datastructureadvancedtree/bitree.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/">





  <title>数据结构之高级树 | Lu QiLin's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Lu QiLin's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-technology">
          <a href="/categories/Technology/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Technology
          </a>
        </li>
      
        
        <li class="menu-item menu-item-notes">
          <a href="/categories/Notes/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Notes
          </a>
        </li>
      
        
        <li class="menu-item menu-item-personal">
          <a href="/categories/Personal/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Personal
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://luqilinok.github.io/2019/06/26/datastructureadvancedtree/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LuQiLin">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lu QiLin's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">数据结构之高级树</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-06-26T14:05:03+08:00">
                2019-06-26
              </time>
            

            

            
          </span>
          
  <span class="post-updated">
    &nbsp; | &nbsp; 更新于
    <time itemprop="dateUpdated" datetime="2019-06-28T08:34:12+08:00" content="2019-06-28">
      2019-06-28
    </time>
  </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Notes/" itemprop="url" rel="index">
                    <span itemprop="name">Notes</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>线索二叉树、哈夫曼树、AVL树、伸展树、B树、B+树、B*树、R树、红黑树概念与总结</p>
<a id="more"></a>
<hr>
<h1 id="线索二叉树"><a href="#线索二叉树" class="headerlink" title="线索二叉树"></a>线索二叉树</h1><p>线索二叉树主要用来解决树的遍历问题，普通的二叉树遍历时，需要使用栈结果来做重复的操作。线索二叉树不需要如此，在遍历的同时，使用二叉树中空闲的内存记录某些结点的前驱和后继元素的位置（不是全部）。这样在算法后期需要遍历二叉树时，就可以利用保存的结点信息，提高了遍历的效率。使用这种方法构建的二叉树称为“线索二叉树”。</p>
<p>结点结构体：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Link 0 <span class="comment">//表示该节点有非空孩子节点</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Thread 1 <span class="comment">//表示该节点有后续节点（对于右子树来说）</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">BT_Thread_Node</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"></span><br><span class="line">    T Data;</span><br><span class="line"></span><br><span class="line">    BT_Thread_Node *Left_Child;</span><br><span class="line"></span><br><span class="line">    BT_Thread_Node *Right_Child;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> Ltag;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> Rtag;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>对二叉树进行线索化：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">Thread_Binary_tree</span>&lt;T&gt;:</span>:InOrder_Thread_Op(BT_Thread_Node&lt;T&gt; *&amp;Tree)</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (Tree == <span class="literal">NULL</span>) <span class="comment">//空返回上一节点</span></span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    InOrder_Thread_Op(Tree-&gt;Left_Child); <span class="comment">//左</span></span><br><span class="line">    <span class="keyword">if</span> (Tree-&gt;Left_Child == <span class="literal">NULL</span>)        <span class="comment">//根</span></span><br><span class="line">    &#123;</span><br><span class="line">        Tree-&gt;Ltag = Thread;</span><br><span class="line">        Tree-&gt;Left_Child = Pre_Node;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (Pre_Node != <span class="literal">NULL</span> &amp;&amp; Pre_Node-&gt;Right_Child == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        Pre_Node-&gt;Rtag = Thread;</span><br><span class="line">        Pre_Node-&gt;Right_Child = Tree;</span><br><span class="line">    &#125;</span><br><span class="line">    Pre_Node = Tree;</span><br><span class="line">    InOrder_Thread_Op(Tree-&gt;Right_Child); <span class="comment">//右</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>进行线索化的树结构如图所示：</p>
<p><img src="/2019/06/26/datastructureadvancedtree/C:/Blog\myblog\source\_posts\datastructureadvancedtree\bitree.png" alt="线索二叉树"></p>
<p>中序遍历线索二叉树</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">Thread_Binary_tree</span>&lt;T&gt;:</span>:_InOrder_Op(BT_Thread_Node&lt;T&gt; *&amp;Tree)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (Tree == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    BT_Thread_Node&lt;T&gt; *Cur_Node = Tree;</span><br><span class="line">    <span class="keyword">while</span> (Cur_Node) <span class="comment">//当前节点不能为空</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">while</span> (Cur_Node-&gt;Ltag == Link) <span class="comment">//节点有左树时，寻找最左端的树</span></span><br><span class="line">        &#123;</span><br><span class="line">            Cur_Node = Cur_Node-&gt;Left_Child;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; Cur_Node-&gt;Data &lt;&lt; <span class="string">" "</span>;</span><br><span class="line">        <span class="keyword">while</span> (Cur_Node &amp;&amp; Cur_Node-&gt;Rtag == Thread) <span class="comment">//节点非空并且右树是线索树时查找最前的一个线索树节点</span></span><br><span class="line">        &#123;</span><br><span class="line">            Cur_Node = Cur_Node-&gt;Right_Child;</span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; Cur_Node-&gt;Data &lt;&lt; <span class="string">" "</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        Cur_Node = Cur_Node-&gt;Right_Child; <span class="comment">//右树不是线索树，查找该节点的右孩子节点</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="哈夫曼树"><a href="#哈夫曼树" class="headerlink" title="哈夫曼树"></a>哈夫曼树</h1><p>首先介绍几个名词。</p>
<p>1.路径：在一棵树中，一个结点到另一个结点之间的通路，称为路径。</p>
<p>2.路径长度：在一条路径中，每经过一个结点，路径长度都要加 1 。</p>
<p>3.结点的权：给每一个结点赋予一个新的数值，被称为这个结点的权。</p>
<p>4.结点的带权路径长度：指的是从根结点到该结点之间的路径长度与该结点的权的乘积。</p>
<p>5.树的带权路径长度为树中所有叶子结点的带权路径长度之和。通常记作 “WPL” 。</p>
<h2 id="什么是哈夫曼树？"><a href="#什么是哈夫曼树？" class="headerlink" title="什么是哈夫曼树？"></a>什么是哈夫曼树？</h2><p>1.当用 n 个结点（都做叶子结点且都有各自的权值）试图构建一棵树时，如果构建的这棵树的<strong>带权路径长度最小</strong>，称这棵树为“最优二叉树”，有时也叫“赫夫曼树”或者“哈夫曼树”。<br>2.在构建哈弗曼树时，要使树的带权路径长度最小，只需要遵循一个原则，那就是：<strong>权重越大的结点离树根越近</strong>。</p>
<h2 id="构建哈夫曼树"><a href="#构建哈夫曼树" class="headerlink" title="构建哈夫曼树"></a>构建哈夫曼树</h2><p>对于给定的有各自权值的 n 个结点，构建哈夫曼树有一个行之有效的办法：<br>1.在 n 个权值中选出两个最小的权值，对应的两个结点组成一个新的二叉树，且新二叉树的根结点的权值为左右孩子权值的和；<br>2.在原有的 n 个权值中删除那两个最小的权值，同时将新的权值加入到 n–2 个权值的行列中，以此类推；<br>3.重复 1 和 2 ，直到所以的结点构建成了一棵二叉树为止，这棵树就是哈夫曼树。</p>
<p><img src="/2019/06/26/datastructureadvancedtree/huffm_tree.png" alt="哈夫曼树"></p>
<h2 id="哈夫曼树算法"><a href="#哈夫曼树算法" class="headerlink" title="哈夫曼树算法"></a>哈夫曼树算法</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> weight; <span class="comment">// 权重</span></span><br><span class="line">    <span class="keyword">int</span> parent; <span class="comment">// 父节点在数组中的位置下标</span></span><br><span class="line">    <span class="keyword">int</span> left;   <span class="comment">// 左节点在数组中的位置下标</span></span><br><span class="line">    <span class="keyword">int</span> right;  <span class="comment">// 右节点在数组中的位置下标</span></span><br><span class="line">&#125; HTNode, *HuffmanTree;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Select</span><span class="params">(HuffmanTree HT, <span class="keyword">int</span> *m1, <span class="keyword">int</span> *m2, <span class="keyword">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> min1 = INT8_MAX;</span><br><span class="line">    <span class="keyword">int</span> min2 = INT8_MAX;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (HT[i].parent != <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (HT[i].weight &lt; min1)</span><br><span class="line">        &#123;</span><br><span class="line">            min2 = min1;</span><br><span class="line">            min1 = HT[i].weight;</span><br><span class="line">            *m2 = *m1;</span><br><span class="line">            *m1 = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (HT[i].weight &gt;= min1 &amp;&amp; HT[i].weight &lt; min2)</span><br><span class="line">        &#123;</span><br><span class="line">            min2 = HT[i].weight;</span><br><span class="line">            *m2 = i;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">createHuffmanTree</span><span class="params">(HuffmanTree *HT, <span class="keyword">int</span> w[], <span class="keyword">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (n &lt;= <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">2</span> * n - <span class="number">1</span>; <span class="comment">// 总节点数</span></span><br><span class="line">    *HT = (HuffmanTree)<span class="built_in">malloc</span>((m + <span class="number">1</span>) * <span class="keyword">sizeof</span>(HTNode));</span><br><span class="line">    HuffmanTree p = *HT;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= m; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        p[i].weight = i &lt;= n ? w[i - <span class="number">1</span>] : <span class="number">0</span>;</span><br><span class="line">        p[i].parent = <span class="number">0</span>;</span><br><span class="line">        p[i].right = <span class="number">0</span>;</span><br><span class="line">        p[i].left = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 构建哈夫曼树</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = n + <span class="number">1</span>; i &lt;= m; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> m1, m2;</span><br><span class="line">        Select(*HT, &amp;m1, &amp;m2, i - <span class="number">1</span>);</span><br><span class="line">        p[m1].parent = p[m2].parent = i;</span><br><span class="line">        p[i].left = m1;</span><br><span class="line">        p[i].left = m2;</span><br><span class="line">        p[i].weight = p[m1].weight + p[m2].weight;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">(HuffmanTree HT, <span class="keyword">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">2</span> * n - <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= m; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">cout</span> &lt;&lt; (HT + i)-&gt;weight &lt;&lt; <span class="string">" "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> w[<span class="number">5</span>] = &#123;<span class="number">2</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">8</span>&#125;;</span><br><span class="line">    HuffmanTree HT;</span><br><span class="line">    createHuffmanTree(&amp;HT, w, <span class="number">5</span>);</span><br><span class="line">    print(HT, <span class="number">5</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="哈夫曼编码"><a href="#哈夫曼编码" class="headerlink" title="哈夫曼编码"></a>哈夫曼编码</h2><p>哈夫曼编码就是在哈夫曼树的基础上构建的，这种编码方式的最大优点就是用最少的字符包含最多的信息内容。文本中字符出现的次数越多，在哈夫曼树中的体现就越接近树根。编码的长度越短。</p>
<hr>
<h1 id="AVL树"><a href="#AVL树" class="headerlink" title="AVL树"></a>AVL树</h1><p>1.AVL树又称为高度平衡的二叉搜索树。它能保持二叉树的高度平衡，尽量降低二叉树的高度，减少树的平均搜索长度。</p>
<p>2.ALV树的性质：</p>
<p>​    （1）左子树和右子树的高度之差的绝对值不超过1；</p>
<p>​    （2）树中的每个左子树和右子树都是AVL树；</p>
<p>​    （3）每个结点都有一个平衡因子（balance factor），任一结点的平衡因子是-1，0，1.（每个结点的平衡因子等于左子树的高度减去右子树的高度）。</p>
<h2 id="基本术语和操作"><a href="#基本术语和操作" class="headerlink" title="基本术语和操作"></a>基本术语和操作</h2><p>有四种情况可能会导致AVL树失衡，分别是：</p>
<p>（1）LL：插入一个新节点到根节点的左子树（Left）的左子树（Left），导致根节点的平衡因子由1变为2；<br>（2）RR：插入一个新节点到根节点的右子树（Right）的右子树（Right），导致根节点的平衡因子由-1变为-2；<br>（3）LR：插入一个新节点到根节点的左子树（Left）的右子树（Right），导致根节点的平衡因子由1变为2；<br>（4）RL：插入一个新节点到根节点的右子树（Right）的左子树（Left），导致根节点的平衡因子由-1变为-2；</p>
<p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_1.jpg" alt></p>
<p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_2.jpg" alt></p>
<h2 id="LL左旋转"><a href="#LL左旋转" class="headerlink" title="LL左旋转"></a>LL左旋转</h2><p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_ll.jpg" alt></p>
<h2 id="RR右旋转"><a href="#RR右旋转" class="headerlink" title="RR右旋转"></a>RR右旋转</h2><p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_rr.jpg" alt></p>
<h1 id="LR左右旋转"><a href="#LR左右旋转" class="headerlink" title="LR左右旋转"></a>LR左右旋转</h1><p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_lr.jpg" alt></p>
<h2 id="RL右左旋转"><a href="#RL右左旋转" class="headerlink" title="RL右左旋转"></a>RL右左旋转</h2><p><img src="/2019/06/26/datastructureadvancedtree/avl_tree_rl.jpg" alt></p>
<h2 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">AVLTreeNode</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    AVLTreeNode *left;</span><br><span class="line">    AVLTreeNode *right;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> val;</span><br><span class="line">    <span class="keyword">int</span> height;</span><br><span class="line">    AVLTreeNode(<span class="keyword">int</span> val) : val(val), left(<span class="literal">NULL</span>),</span><br><span class="line">                           right(<span class="literal">NULL</span>), height(<span class="number">0</span>) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">height</span><span class="params">(<span class="keyword">const</span> AVLTreeNode *node)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (node == <span class="literal">NULL</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> node-&gt;height;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">leftRotate</span><span class="params">(AVLTreeNode *node)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    AVLTreeNode *tmp = node-&gt;left;</span><br><span class="line">    node-&gt;left = tmp-&gt;right;</span><br><span class="line">    tmp-&gt;right = node;</span><br><span class="line"></span><br><span class="line">    node-&gt;height = max(height(node-&gt;left), height(node-&gt;right)) + <span class="number">1</span>;</span><br><span class="line">    tmp-&gt;height = max(height(tmp-&gt;left), height(tmp-&gt;right)) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> tmp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">rightRotate</span><span class="params">(AVLTreeNode *node)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    AVLTreeNode *tmp = node-&gt;right;</span><br><span class="line">    node-&gt;right = tmp-&gt;left;</span><br><span class="line">    tmp-&gt;left = node;</span><br><span class="line"></span><br><span class="line">    node-&gt;height = max(height(node-&gt;left), height(node-&gt;right)) + <span class="number">1</span>;</span><br><span class="line">    tmp-&gt;height = max(height(tmp-&gt;left), height(tmp-&gt;right)) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> tmp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">leftRightRotate</span><span class="params">(AVLTreeNode *node)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    node-&gt;left = leftRotate(node-&gt;left);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> rightRotate(node);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">rightLeftRotate</span><span class="params">(AVLTreeNode *node)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    node-&gt;right = rightRotate(node-&gt;right);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> leftRotate(node);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">insertNode</span><span class="params">(AVLTreeNode *root, <span class="keyword">int</span> val)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (root == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        root = <span class="keyword">new</span> AVLTreeNode(val);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (val &lt; root-&gt;val)</span><br><span class="line">    &#123;</span><br><span class="line">        root-&gt;left = insertNode(root-&gt;left, val);</span><br><span class="line">        <span class="keyword">if</span> (height(root-&gt;left) - height(root-&gt;right) == <span class="number">2</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (val &lt; root-&gt;left-&gt;val)</span><br><span class="line">            &#123;</span><br><span class="line">                root = leftRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                root = leftRightRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (val &gt; root-&gt;val)</span><br><span class="line">    &#123;</span><br><span class="line">        root-&gt;right = insertNode(root-&gt;right, val);</span><br><span class="line">        <span class="keyword">if</span> (height(root-&gt;right) - height(root-&gt;left) == <span class="number">2</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (val &gt; root-&gt;right-&gt;val)</span><br><span class="line">            &#123;</span><br><span class="line">                root = rightRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                root = rightLeftRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    root-&gt;height = max(height(root-&gt;left), height(root-&gt;right)) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">maxNode</span><span class="params">(AVLTreeNode *root)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    AVLTreeNode *maxAN = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (root != <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        maxAN = root;</span><br><span class="line">        root = root-&gt;right;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> maxAN;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">minNode</span><span class="params">(AVLTreeNode *root)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    AVLTreeNode *maxAN = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (root != <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        maxAN = root;</span><br><span class="line">        root = root-&gt;left;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> maxAN;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">AVLTreeNode *<span class="title">deleteNode</span><span class="params">(AVLTreeNode *root, <span class="keyword">int</span> val)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (root == <span class="literal">NULL</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (val &lt; root-&gt;val)</span><br><span class="line">    &#123;</span><br><span class="line">        root-&gt;left = deleteNode(root-&gt;left, val);</span><br><span class="line">        <span class="keyword">if</span> (height(root-&gt;right) - height(root-&gt;left) == <span class="number">2</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            AVLTreeNode *r = root-&gt;right;</span><br><span class="line">            <span class="keyword">if</span> (height(r-&gt;right) &gt; height(r-&gt;left))</span><br><span class="line">            &#123;</span><br><span class="line">                root = rightLeftRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                root = rightRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (val &gt; root-&gt;val)</span><br><span class="line">    &#123;</span><br><span class="line">        root-&gt;right = insertNode(root-&gt;right, val);</span><br><span class="line">        <span class="keyword">if</span> (height(root-&gt;left) - height(root-&gt;right) == <span class="number">2</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            AVLTreeNode *r = root-&gt;right;</span><br><span class="line">            <span class="keyword">if</span> (height(r-&gt;right) &gt; height(r-&gt;left))</span><br><span class="line">            &#123;</span><br><span class="line">                root = leftRightRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                root = leftRotate(root);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (root-&gt;left != <span class="literal">NULL</span> &amp;&amp; root-&gt;right != <span class="literal">NULL</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">if</span> (height(root-&gt;left) &gt; height(root-&gt;right))</span><br><span class="line">            &#123;</span><br><span class="line">                AVLTreeNode *maxAN = maxNode(root-&gt;left);</span><br><span class="line">                root-&gt;val = maxAN-&gt;val;</span><br><span class="line">                root-&gt;left = deleteNode(root-&gt;left, maxAN-&gt;val);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">            &#123;</span><br><span class="line">                AVLTreeNode *maxAN = minNode(root-&gt;right);</span><br><span class="line">                root-&gt;val = maxAN-&gt;val;</span><br><span class="line">                root-&gt;right = deleteNode(root-&gt;right, maxAN-&gt;val);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            AVLTreeNode *tmp = root;</span><br><span class="line">            root = root-&gt;left != <span class="literal">NULL</span> ? root-&gt;left : root-&gt;right;</span><br><span class="line">            <span class="built_in">free</span>(tmp);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<h1 id="伸展树"><a href="#伸展树" class="headerlink" title="伸展树"></a>伸展树</h1><p>· 伸展树的主要特点是不会保证树是一直平衡的，但各种操作的平摊时间复杂度是O(log(n)),因而，从平摊复杂度上看，二叉查找树也是一种平衡二叉树。另外，相比于其他树状数据结构（如红黑树、AVL树等），伸展树的空间要求与编程复杂度要小得多。</p>
<p>· 伸展树的出发点是这样的：考虑到局部性原理（刚被访问的内容下次可能仍然会被访问，查找次数多的内容下一次可能会被访问），为了使整个查找时间更小，被查频率高的那些结点应当经常处于靠近树根的位置。这样，很容易得到以下这个方案：每次查找节点之后对树进行重构，把被查找的节点搬移到树根，这种自调形式的二叉树就是伸展树。每次对伸展树进行从操作后，它均会通过旋转的方法把被访问节点旋转到树根的位置。</p>
<p>· 为了将当前被访问节点旋转到树根，我们通常将节点自底向上旋转，直至该节点成为树根为止。“旋转”的巧妙之处就是在不打乱数列中数据大小关系（指中序遍历结果是全序的）情况下，所有基本操作的平摊时间复杂度仍为O（log(n)）。</p>
<hr>
<h1 id="B树"><a href="#B树" class="headerlink" title="B树"></a>B树</h1><p>1.动态查找树有二叉查找树（BST）、平衡二叉查找树（BBST）、红黑树（RBT）、B-Tree/B+Tree/B*-tree（B树）。</p>
<p>2.B树主要应用与存储系统中，在大规模的存储系统中，文件的索引节点非常多，而如果使用平衡二叉树这样的结构做搜索，元素过多，查找可能就会退化成线性查找了，这样的二叉树由于深度过大而造成磁盘I/O读写过于频繁，进而导致查询效率低下。B树是通过减少树的高度，增加节点元素，提高查询效率的。</p>
<h2 id="规则"><a href="#规则" class="headerlink" title="规则"></a>规则</h2><p>1.树中的每个节点最多拥有m个子节点且m&gt;=2，空树除外（注：m阶代表一个树节点最多有多少个查找路径，m阶=m路，当m=2则是二叉树，m=3则是三叉树）；</p>
<p>2.除了根节点之外每个节点的关键字数量大于等于ceil(m/2)-1小于m-1个；</p>
<p>3.所有叶子节点均在同一层、叶子节点除了包含了关键字和关键字记录的指针外也有指向其子节点的指针只不过其指针地址都为null，对应下图最后一层节点的空格子。</p>
<p>4.如果一个非叶节点有N个子节点，则该节点的关键字数等于N-1；</p>
<p>5.所有节点关键字是按递增次序排列，并且遵循左小右大原则。</p>
<hr>
<h2 id="B树查询流程"><a href="#B树查询流程" class="headerlink" title="B树查询流程"></a>B树查询流程</h2><p><img src="/2019/06/26/datastructureadvancedtree/b-tree.jpg" alt="B-tree"></p>
<p>比如要查找字母E，所有字母按照字典序排序，查找流程如下：</p>
<p>（1）获取根节点的关键字进行比较，当前根节点关键字为M，E要小于M，所以往找到指向左边的子节点（二分法规则，左小右大，左边放小于当前节点值的子节点、右边放大于当前节点值的子节点）；</p>
<p>（2）拿到关键字D和G，D&lt;E&lt;G 所以直接找到D和G中间的节点；</p>
<p>（3）拿到E和F，因为E=E 所以直接返回关键字和指针信息（如果树结构里面没有包含所要查找的节点则返回null）。</p>
<h2 id="B树插入流程"><a href="#B树插入流程" class="headerlink" title="B树插入流程"></a>B树插入流程</h2><p>定义一个5阶树（平衡5路查找树;），现在我们要把3、8、31、11、23、29、50、28 这些数字构建出一个5阶树出来；<br>遵循规则：</p>
<p>（1）当前是要组成一个5路查找树，那么此时m=5,关键字数必须大于等于cei(5/2)小于等于5-1（关键字数小于cei(5/2) -1就要进行节点合并，大于5-1就要进行节点拆分,非根节点关键字数&gt;=2）；</p>
<p>（2）满足节点本身比左边节点大，比右边节点小的排序规则。</p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-tree-insert-1.jpg" alt></p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-tree-insert-2.jpg" alt></p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-tree-insert-3.jpg" alt></p>
<h2 id="B树删除流程"><a href="#B树删除流程" class="headerlink" title="B树删除流程"></a>B树删除流程</h2><p>1.当前是要组成一个5路查找树，那么此时m=5,关键字数必须大于等于cei(5/2)-1，小于等于5-1，非根节点关键字数大于2；</p>
<p>2.满足节点本身比左边节点大，比右边节点小的排序规则;</p>
<p>3.关键字数小于二时先从子节点取，子节点没有符合条件时就向向父节点取，取中间值往父节点放；</p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-tree-delete.jpg" alt></p>
<h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><p><img src="/2019/06/26/datastructureadvancedtree/b-tree-disk.jpg" alt></p>
<p>B树的结构定义：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> file_num; <span class="comment">// 文件数</span></span><br><span class="line">    <span class="keyword">char</span> *file_name[max_file_num]; <span class="comment">// 文件名</span></span><br><span class="line">    BTNode *BTptr[max_file_num+<span class="number">1</span>]; <span class="comment">// 指向子节点的指针</span></span><br><span class="line">    FILE_HARD_ADDR offset[max_file_num]; <span class="comment">// 文件在硬盘中的存储位置</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>一般来说，一个磁盘块的大小是4K，磁盘进行IO请求时都是以块为单位的，也就是B树作为磁盘或者数据库的索引结构时，其关键字的个数限制在磁盘块范围，B树节点内部采用二分查找，由于其层级相对于平衡二叉树来说减少了很多，查询效率也增大了很多。</p>
<hr>
<h1 id="B-树"><a href="#B-树" class="headerlink" title="B+树"></a>B+树</h1><p>B+树是B树的一种变形，更适合应用于实际应用中操作系统的文件索引和数据库索引。定义如下：</p>
<p>1.除根节点外的内部节点，每个节点最多有m个关键字，最少有ceil(m/2)个关键字，其中每个关键字对应一个子树；</p>
<p>2.所有的叶子节点包含了全部的关键字以及这些关键字指向文件的指针，并且：</p>
<p>(1).所有叶子节点中的关键字按大小顺序排列；</p>
<p>(2).相邻的叶子节点顺序链接（相当于是构成了一个顺序链表）；</p>
<p>(3).所有叶子节点在同一层。</p>
<p>3.所有分支节点的关键字都是对应子树中关键字的最大（或最小）值。</p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-plus-tree.jpg" alt></p>
<p>B+树相对B树来说，有以下的特点：</p>
<p>1.B+树的磁盘读写代价更低：</p>
<p>​    (1).B+树的内部结点并没有指向关键字具体信息的指针。因此其内部结点相对B 树更小。如果把所有同一内部结点的关键字存放在同一盘块中，那么盘块所能容纳的关键字数量也越多。一次性读入内存中的需要查找的关键字也就越多。相对来说IO读写次数也就降低了。</p>
<p>2.B+树的查询效率更加稳定</p>
<p>​    (1).由于非终结点并不是最终指向文件内容的结点，而只是叶子结点中关键字的索引。所以任何关键字的查找必须走一条从根节点到叶子结点的路。所有关键字查询的路径长度相同，导致每一个数据的查询效率相当。</p>
<hr>
<h1 id="B-树-1"><a href="#B-树-1" class="headerlink" title="B*树"></a>B*树</h1><p>B<em> 树是B+树的变体，在B+树的基础上，B</em> 树增加了非根和非叶子结点指向兄弟结点的指针，B<em> 树关键字初始化个数为ceil(2/3\</em>m)，即磁盘块的最低使用率为2/3。</p>
<p><img src="/2019/06/26/datastructureadvancedtree/b-star-stree.jpg" alt="B*树"></p>
<p>(1).B+树的分裂：当一个结点满时，分配一个新的结点，并将原结点中1/2的数据复制到新结点，最后在父结点中增加新结点的指针；</p>
<p>B+树的分裂只影响原结点和父结点，而不会影响兄弟结点，所以它不需要指向兄弟的指针。<br>(2)B树的分裂：当一个结点满时，如果它的下一个兄弟结点未满，那么将一部分数据移到兄弟结点中，再在原结点插入关键字，最后修改父结点中兄弟结点的关键字（因为兄弟结点的关键字范围改变了）；如果兄弟也满了，则在原结点与兄弟结点之间增加新结点，并各复制1/3的数据到新结点，最后在父结点增加新结点的指针。所以，B树分配新结点的概率要比B+树要低，空间使用率更高。</p>
<hr>
<h1 id="R树"><a href="#R树" class="headerlink" title="R树"></a>R树</h1><p>R树相对于B树或者B+树来说，主要解决的是多维空间查找问题。B树和B+树可以很好的处理一维空间存储的问题，如果涉及到多维空间上的查找，就要用到R树。R树在数据库等领域做出的功绩是非常显著的。它很好的解决了在高维空间搜索等问题。举个R树在现实领域中能够解决的例子：查找20英里以内所有的餐厅。如果没有R树你会怎么解决？一般情况下我们会把餐厅的坐标(x,y)分为两个字段存放在数据库中，一个字段记录经度，另一个字段记录纬度。这样的话我们就需要遍历所有的餐厅获取其位置信息，然后计算是否满足要求。如果一个地区有100家餐厅的话，我们就要进行100次位置计算操作了，如果应用到谷歌地图这种超大数据库中，这种方法便必定不可行了。</p>
<p><img src="/2019/06/26/datastructureadvancedtree/R-tree.jpg" alt="R树"></p>
<hr>
<h1 id="红黑树"><a href="#红黑树" class="headerlink" title="红黑树"></a>红黑树</h1><p>红黑树本质上也是一棵二叉查找树，但它在二叉查找树的基础上增加了着色和相关的性质使得红黑树相对平衡。红黑树的每个节点上都有存储位表示节点的颜色，可以是红(Red)或黑(Black)。</p>
<h2 id="性质"><a href="#性质" class="headerlink" title="性质"></a>性质</h2><ul>
<li>(1)每个节点或者是黑色，或者是红色；</li>
<li>(2)根节点是黑色；</li>
<li>(3)每个叶子结点都是黑色（NULL结点是黑结点）；</li>
<li>(4)如果一个节点是红色的，则他的子结点必须是黑色的；</li>
<li>(5)从一个节点到该结点的子孙结点的所有路径上包含相同数目的黑结点。</li>
</ul>
<p><img src="/2019/06/26/datastructureadvancedtree/rb-tree.png" alt="红黑树"></p>
<h2 id="时间复杂度"><a href="#时间复杂度" class="headerlink" title="时间复杂度"></a>时间复杂度</h2><p>红黑树的时间复杂度为：O(logn)</p>
<h2 id="添加"><a href="#添加" class="headerlink" title="添加"></a>添加</h2><p>将一个节点插入到红黑树中，需要执行哪些步骤呢？首先，将红黑树当作一颗二叉查找树，将节点插入；然后，将节点着色为红色；最后，通过旋转和重新着色等方法来修正该树，使之重新成为一颗红黑树。详细描述如下：</p>
<ul>
<li>第一步: 将红黑树当作一颗二叉查找树，将节点插入。<ul>
<li>红黑树本身就是一颗二叉查找树，将节点插入后，该树仍然是一颗二叉查找树。也就意味着，树的键值仍然是有序的。此外，无论是左旋还是右旋，若旋转之前这棵树是二叉查找树，旋转之后它一定还是二叉查找树。这也就意味着，任何的旋转和重新着色操作，都不会改变它仍然是一颗二叉查找树的事实。</li>
</ul>
</li>
<li>第二步：将插入的节点着色为”红色”，将插入结点变为红色不会违背第(1)条性质。</li>
<li>第三步: 通过一系列的旋转或着色等操作，使之重新成为一颗红黑树。<ul>
<li>对于”性质(1)”，显然不会违背了。因为我们已经将它涂成红色了；</li>
<li>对于”性质(2)”，显然也不会违背。在第一步中，我们是将红黑树当作二叉查找树，然后执行的插入操作。而根据二叉查找数的特点，插入操作不会改变根节点。所以，根节点仍然是黑色；</li>
<li>对于”性质(3)”，显然不会违背了。这里的叶子节点是指的空叶子节点，插入非空节点并不会对它们造成影响；</li>
<li>对于”性质(4)”，是有可能违背的！<br>可以将“当节点被着色为红节点，并插入二叉树”划分为三种情况处理。</li>
</ul>
</li>
<li>第一种情况：被插入的节点是根节点 –&gt; 处理方法：直接把此节点涂为黑色；</li>
<li>第二种情况：被插入的节点的父节点是黑色 –&gt; 处理方法：什么也不需要做。节点被插入后，仍然是红黑树；</li>
<li>第三种情况：被插入的节点的父节点是红色 –&gt; 那么，该情况与红黑树的“性质(4)”相冲突。这种情况下，被插入节点是一定存在非空祖父节点的；进一步的讲，被插入节点也一定存在叔叔节点(即使叔叔节点为空，我们也视之为存在，空节点本身就是黑色节点)。理解这点之后，我们依据”叔叔节点的情况”，将这种情况进一步划分为3种情况(Case)。</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">现象说明</th>
<th style="text-align:left">处理策略</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Case1</td>
<td style="text-align:left">当前节点的父节点是红色，且当前节点的祖父节点的另一个子节点（叔叔节点）也是红色。</td>
<td>(01) 将“父节点”设为黑色。 (02) 将“叔叔节点”设为黑色。 (03) 将“祖父节点”设为“红色”。 (04) 将“祖父节点”设为“当前节点”(红色节点)；即，之后继续对“当前节点”进行操作。</td>
</tr>
<tr>
<td style="text-align:left">Case2</td>
<td style="text-align:left">当前节点的父节点是红色，叔叔节点是黑色，且当前节点是其父节点的右孩子</td>
<td>(01) 将“父节点”作为“新的当前节点”。 (02) 以“新的当前节点”为支点进行左旋。</td>
</tr>
<tr>
<td style="text-align:left">Case3</td>
<td style="text-align:left">当前节点的父节点是红色，叔叔节点是黑色，且当前节点是其父节点的左孩子</td>
<td>(01) 将“父节点”设为“黑色”。 (02) 将“祖父节点”设为“红色”； (03) 以“祖父节点”为支点进行右旋。</td>
</tr>
</tbody>
</table>
<h2 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h2><p>将红黑树内的某一个节点删除。需要执行的操作依次是：首先，将红黑树当作一颗二叉查找树，将该节点从二叉查找树中删除；然后，通过”旋转和重新着色”等一系列来修正该树，使之重新成为一棵红黑树。详细描述如下：</p>
<p>第一步：将红黑树当作一颗二叉查找树，将节点删除。</p>
<ul>
<li>被删除节点没有儿子，即为叶节点。那么，直接将该节点删除就OK了。</li>
<li>被删除节点只有一个儿子。那么，直接删除该节点，并用该节点的唯一子节点顶替它的位置。</li>
<li><p>被删除节点有两个儿子。那么，先找出它的后继节点；然后把“它的后继节点的内容”复制给“该节点的内容”；之后，删除“它的后继节点”。在这里，后继节点相当于替身，在将后继节点的内容复制给”被删除节点”之后，再将后继节点删除。这样就巧妙的将问题转换为”删除后继节点”的情况了，下面就考虑后继节点。 在”被删除节点”有两个非空子节点的情况下，它的后继节点不可能是双子非空。既然”的后继节点”不可能双子都非空，就意味着”该节点的后继节点”要么没有儿子，要么只有一个儿子。若没有儿子，则按”情况一”进行处理；若只有一个儿子，则按”情况二 “进行处理。</p>
</li>
<li><p>第二步：通过”旋转和重新着色”等一系列来修正该树，使之重新成为一棵红黑树。<br>在第一步中”将红黑树当作一颗二叉查找树，将节点删除”后，可能违反”性质(2)、(4)、(5)”三个性质。第二步需要解决上面的三个问题，进而保持红黑树的全部性质。<br>为了便于分析，我们假设”x包含一个额外的黑色”(x原本的颜色还存在)，这样就不会违反”性质(5)”。为什么呢？<br>我们知道：删除节点y之后，x占据了原来节点y的位置。 既然删除y(y是黑色)，意味着减少一个黑色节点；那么，再在该位置上增加一个黑色即可。这样，当我们假设”x包含一个额外的黑色”，就正好弥补了”删除y所丢失的黑色节点”，也就不会违反”性质(5)”。 因此，假设”x包含一个额外的黑色”(x原本的颜色还存在)，这样就不会违反”性质(5)”。<br>现在，x不仅包含它原本的颜色属性，x还包含一个额外的黑色。即x的颜色属性是”红+黑”或”黑+黑”，它违反了”性质(1)”。<br>现在，我们面临的问题，由解决”违反了性质(2)、(4)、(5)三个性质”转换成了”解决违反性质(1)、(2)、(4)三个性质”。RB-DELETE-FIXUP需要做的就是通过算法恢复红黑树的性质(1)、(2)、(4)。RB-DELETE-FIXUP的思想是：将x所包含的额外的黑色不断沿树上移(向根方向移动)，直到出现下面的姿态：</p>
</li>
<li>x指向一个”红+黑”节点。此时，将x设为一个”黑”节点即可。</li>
<li>x指向根。此时，将x设为一个”黑”节点即可。</li>
<li>非前面两种姿态。</li>
</ul>
<p>将上面的姿态，可以概括为3种情况：</p>
<ul>
<li>情况说明：x是“红+黑”节点 –&gt; 处理方法：直接把x设为黑色，结束。此时红黑树性质全部恢复。</li>
<li>情况说明：x是“黑+黑”节点，且x是根 –&gt; 处理方法：什么都不做，结束。此时红黑树性质全部恢复。</li>
<li>情况说明：x是“黑+黑”节点，且x不是根 –&gt; 这种情况又可以划分为4种子情况。这4种子情况如下表所示：</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">现象说明</th>
<th style="text-align:left">处理策略</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Case1</td>
<td style="text-align:left">x是”黑+黑”节点，x的兄弟节点是红色。(此时x的父节点和x的兄弟节点的子节点都是黑节点)。</td>
<td>(01) 将x的兄弟节点设为“黑色”。 (02) 将x的父节点设为“红色”。 (03) 对x的父节点进行左旋。 (04) 左旋后，重新设置x的兄弟节点。</td>
</tr>
<tr>
<td style="text-align:left">Case2</td>
<td style="text-align:left">x是“黑+黑”节点，x的兄弟节点是黑色，x的兄弟节点的两个孩子都是黑色。</td>
<td>(01) 将x的兄弟节点设为“红色”。 (02) 设置“x的父节点”为“新的x节点”。</td>
</tr>
<tr>
<td style="text-align:left">Case3</td>
<td style="text-align:left">x是“黑+黑”节点，x的兄弟节点是黑色；x的兄弟节点的左孩子是红色，右孩子是黑色的。</td>
<td>(01) 将x兄弟节点的左孩子设为“黑色”。 (02) 将x兄弟节点设为“红色”。 (03) 对x的兄弟节点进行右旋。 (04) 右旋后，重新设置x的兄弟节点。</td>
</tr>
<tr>
<td style="text-align:left">Case4</td>
<td style="text-align:left">x是“黑+黑”节点，x的兄弟节点是黑色；x的兄弟节点的右孩子是红色的，x的兄弟节点的左孩子任意颜色。</td>
<td>(01) 将x父节点颜色 赋值给 x的兄弟节点。 (02) 将x父节点设为“黑色”。 (03) 将x兄弟节点的右子节设为“黑色”。 (04) 对x的父节点进行左旋。 (05) 设置“x”为“根节点”。</td>
</tr>
</tbody>
</table>
<hr>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/26/designpatterninterpreter/" rel="next" title="解析器">
                <i class="fa fa-chevron-left"></i> 解析器
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/28/datastructuregraph/" rel="prev" title="数据结构之图算法">
                数据结构之图算法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">LuQiLin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">80</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#线索二叉树"><span class="nav-number">1.</span> <span class="nav-text">线索二叉树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#哈夫曼树"><span class="nav-number">2.</span> <span class="nav-text">哈夫曼树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是哈夫曼树？"><span class="nav-number">2.1.</span> <span class="nav-text">什么是哈夫曼树？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#构建哈夫曼树"><span class="nav-number">2.2.</span> <span class="nav-text">构建哈夫曼树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#哈夫曼树算法"><span class="nav-number">2.3.</span> <span class="nav-text">哈夫曼树算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#哈夫曼编码"><span class="nav-number">2.4.</span> <span class="nav-text">哈夫曼编码</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#AVL树"><span class="nav-number">3.</span> <span class="nav-text">AVL树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#基本术语和操作"><span class="nav-number">3.1.</span> <span class="nav-text">基本术语和操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#LL左旋转"><span class="nav-number">3.2.</span> <span class="nav-text">LL左旋转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RR右旋转"><span class="nav-number">3.3.</span> <span class="nav-text">RR右旋转</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#LR左右旋转"><span class="nav-number">4.</span> <span class="nav-text">LR左右旋转</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#RL右左旋转"><span class="nav-number">4.1.</span> <span class="nav-text">RL右左旋转</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实现"><span class="nav-number">4.2.</span> <span class="nav-text">实现</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#伸展树"><span class="nav-number">5.</span> <span class="nav-text">伸展树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#B树"><span class="nav-number">6.</span> <span class="nav-text">B树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#规则"><span class="nav-number">6.1.</span> <span class="nav-text">规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#B树查询流程"><span class="nav-number">6.2.</span> <span class="nav-text">B树查询流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#B树插入流程"><span class="nav-number">6.3.</span> <span class="nav-text">B树插入流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#B树删除流程"><span class="nav-number">6.4.</span> <span class="nav-text">B树删除流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#特点"><span class="nav-number">6.5.</span> <span class="nav-text">特点</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#B-树"><span class="nav-number">7.</span> <span class="nav-text">B+树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#B-树-1"><span class="nav-number">8.</span> <span class="nav-text">B*树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#R树"><span class="nav-number">9.</span> <span class="nav-text">R树</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#红黑树"><span class="nav-number">10.</span> <span class="nav-text">红黑树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#性质"><span class="nav-number">10.1.</span> <span class="nav-text">性质</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#时间复杂度"><span class="nav-number">10.2.</span> <span class="nav-text">时间复杂度</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加"><span class="nav-number">10.3.</span> <span class="nav-text">添加</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除"><span class="nav-number">10.4.</span> <span class="nav-text">删除</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LuQiLin</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
